﻿using System;
using System.Collections.Generic;
using System.Linq;
using DevelopTool.Bll.LocalDbBlls;
using DevelopTool.Common;
using DevelopTool.Model.CodeModels;
using DevelopTool.Model.CommonModels;
using DevelopTool.Model.DbModels.TableRelation;
using DevelopTool.Model.Enums;
using LiteDB;
using Sunny.UI;

namespace DevelopTool.Bll.DbBlls
{
    public class TableRelationBll: BaseBll
    {
        public TableRelationBll()
        {
            using (var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var col = db.GetCollection<TableRelation>();
                col.EnsureIndex(x => x.NodeName);
                col.EnsureIndex(x => x.NodePid);
                col.EnsureIndex(x => x.NodeType);
                col.EnsureIndex(x => x.CreateTime);
                col.EnsureIndex(x => x.Id);
            }
        }

        public List<TableRelation> GetList(MenuNodeType nodeType = MenuNodeType.Unknown)
        {
            using (var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var models = db.GetCollection<TableRelation>().FindAll();

                if(nodeType != MenuNodeType.Unknown)
                {
                    models = models.Where(it => it.NodeType == nodeType);
                }

                return models.OrderByDescending(it => it.CreateTime).ToList();
            }
        }



        /// <summary>
        /// 根据id获取对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public TableRelation GetById(int id)
        {
            using (var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var col = db.GetCollection<TableRelation>();

                var tableRelation = col.FindOne(it => it.Id == id);

                return tableRelation;

            }
        }


        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="note"></param>
        /// <returns></returns>
        public TableRelation Save(TableRelation tableRelation)
        {
            using (var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var col = db.GetCollection<TableRelation>();
                tableRelation.UpdateTime = DateTime.Now;
                col.Upsert(tableRelation);

                return GetById(tableRelation.Id);
            }
        }


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Delete(int id)
        {
            using (var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var col = db.GetCollection<TableRelation>();
                var tableRelation = col.FindById(id);

                //查询是否有子节点，若有子节点，则不删除。
                if (col.Exists(it => it.NodePid == tableRelation.Id)) return false;

                //删除
                var result = col.Delete(tableRelation.Id);

                return result;
            }
        }


        public void DeleteAll()
        {
            using (var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var col = db.GetCollection<TableRelation>();
                col.Delete(29);
            }
        }

        public void SetRemark(int id,string remark)
        {
            using(var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var col = db.GetCollection<TableRelation>();

                var model = col.FindOne(it => it.Id == id);

                model.Remark = remark;
                model.UpdateTime = DateTime.Now;

                col.Update(model);
            }
        }

        public ManageMenu Get(int id)
        {
            using(var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                return db.GetCollection<TableRelation>().FindOne(it => it.Id == id);
            }
        }

    }
}
